<template>
  <a-modal :maskClosable="false"
    :title='title'
    :width='1280'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>

    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model' :rules='validatorRules'>
        <a-row>
          <a-col span='12'>
            <a-form-model-item label='项目分类' required prop='categoryId'>
              <a-select placeholder="请选择项目分类" v-model="model.categoryId" @change="handleSelectChange">
                <a-select-option v-for="(item, index) in categoryList" :key="index" :value="item.key">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span='12'>
            <a-form-model-item label='分类标签' required prop='categoryTagId'>
              <a-select placeholder="请选择分类标签" v-model="model.categoryTagId">
                <a-select-option v-for="(item, index) in tagList" :key="index" :value="item.key">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col span='12'>
            <a-form-model-item label='项目名称' required prop='project'>
              <a-input placeholder='请输入项目名称' v-model='model.project' />
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col span='12'>
            <a-form-model-item label='项目费用' required prop='amount'>
              <a-input-number
                style='width: 186px'
                v-model='model.amount'
                :default-value="100"
                :formatter="value => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
                :parser="value => value.replace(/\$\s?|(,*)/g, '')"
              />
            </a-form-model-item>
          </a-col>
          <a-col span='12'>
            <a-form-model-item label='服务时间(次/分)'  prop='time'>
              <a-input-number   style='width: 186px' placeholder='请输入项目服务时间(次/分)'  v-model="model.time" :min="1"   />
              <div class='ant-form-explain'>注：服务时间指客户选择服务时间等于或大于服务时间。如不填写，不设服务限时，以完成任务为目标</div>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row justify="start">
          <a-form-item label="项目主图片" required prop='img'  :label-col="{span: 3 }" :wrapperCol="{span: 21 }">
            <j-image-upload :isMultiple="false" v-model='model.img' :bizPath="his"></j-image-upload>
            <div class="ant-form-explain">注：仅支持图片格式且文件大小500K以内，图片尺寸200X200px为最佳。</div>
          </a-form-item>
        </a-row>

        <a-row justify="start">
          <a-form-model-item label='服务详情' required prop='description' :label-col="{span: 3 }" :wrapperCol="{span: 21 }">
            <j-editor v-model="model.description"></j-editor>
          </a-form-model-item>
        </a-row>


      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@api/manage'
import JEditor from '@/components/jeecg/JEditor'

export default {
  name: 'hChDoorServiceProjectModal',
  components: {
    JEditor
  },
  data() {
    return {
      title: '操作',
      visible: false,
      his:"his",
      model: {},
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        categoryId: [{ required: true, message: '请输入项目分类!' }],
        categoryTagId: [{ required: true, message: '请输入项目分类标签!' }],
        project: [{ required: true, message: '请输入项目名称!' }],
        img: [{ required: true, message: '请输入项目主图片!' }],
        amount: [{ required: true, message: '请输入项目费用!' }],
        description: [{ required: true, message: '请输入服务描述大文本!' }],
        remark: [{ required: true, message: '请输入描述!' }],
      },
      url: {
        add: '/ch/door/project',
        edit: '/ch/door/project',
        categoryList: '/his/common/category/selectList/long',
        tagList: '/his/common/category/tag/selectList',
      },
      categoryList:[],
      tagList:[],
      projectType:0,
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    loadCategory(categoryType) {
      const that=this;
      getAction(this.url.categoryList,{categoryType:categoryType}).then((res) => {
        if (res.success && res.result) {
          that.categoryList = res.result
        }
      })
    },
    handleSelectChange(val) {
      const that=this;
      getAction(this.url.tagList,{categoryId:val}).then((res) => {
        if (res.success && res.result) {
           that.tagList = res.result
        }
      })
    },
    add(projectType,categoryType) {
      this.loadCategory(categoryType)
      this.projectType=projectType;
      this.model = Object.assign({}, this.modelDefault)
      this.visible = true
    },
    edit(record,categoryType) {
      this.loadCategory(categoryType)
      this.handleSelectChange(record.categoryId)
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            this.model.projectType=this.projectType;
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }

          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }


  }
}
</script>

<style scoped>

</style>